package com.ruoyi.weixin.mp.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.weixin.mp.form.MaterialFileDeleteForm;
import com.ruoyi.weixin.mp.service.WxAssetsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.material.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 微信公众号素材管理
 * 参考官方文档：https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/New_temporary_materials.html
 * 参考WxJava开发文档：https://github.com/Wechat-Group/WxJava/wiki/MP_永久素材管理
 */
@RestController
@RequestMapping("/manage/wxAssets")
@Api(tags = {"公众号素材-管理后台"})
public class WxAssetsManageController {
	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	WxAssetsService wxAssetsService;

	/**
	 * 获取素材总数
	 *
	 * @return
	 * @throws WxErrorException
	 */
	@GetMapping("/materialCount")
	@ApiOperation(value = "文件素材总数")
	public AjaxResult materialCount(@RequestParam String appid) throws WxErrorException {
		WxMpMaterialCountResult res = wxAssetsService.materialCount(appid);
		return AjaxResult.success(res);
	}

	/**
	 * 获取素材总数
	 *
	 * @return
	 * @throws WxErrorException
	 */
	@GetMapping("/materialNewsInfo")
	@ApiOperation(value = "图文素材总数")
	public AjaxResult materialNewsInfo(@RequestParam String appid,String mediaId) throws WxErrorException {
		WxMpMaterialNews res = wxAssetsService.materialNewsInfo(appid,mediaId);
		return AjaxResult.success(res);
	}


	/**
	 * 根据类别分页获取非图文素材列表
	 *
	 * @param type
	 * @param page
	 * @return
	 * @throws WxErrorException
	 */
	@GetMapping("/materialFileBatchGet")
	@PreAuthorize("@ss.hasPermi('wx:wxassets:list')")
	@ApiOperation(value = "根据类别分页获取非图文素材列表")
	public AjaxResult materialFileBatchGet(@RequestParam String appid,
										   @RequestParam(defaultValue = "image") String type,
										   @RequestParam(defaultValue = "1") int page) throws WxErrorException {
		WxMpMaterialFileBatchGetResult res = wxAssetsService.materialFileBatchGet(appid,type,page);
		return AjaxResult.success(res);
	}

	/**
	 * 分页获取图文素材列表
	 *
	 * @param page
	 * @return
	 * @throws WxErrorException
	 */
	@GetMapping("/materialNewsBatchGet")
	@PreAuthorize("@ss.hasPermi('wx:wxassets:list')")
	@ApiOperation(value = "分页获取图文素材列表")
	public AjaxResult materialNewsBatchGet(@RequestParam String appid,
										   @RequestParam(defaultValue = "1") int page) throws WxErrorException {
		WxMpMaterialNewsBatchGetResult res = wxAssetsService.materialNewsBatchGet(appid,page);
		return AjaxResult.success(res);
	}

	/**
	 * 添加图文永久素材
	 *
	 * @param articles
	 * @return
	 * @throws WxErrorException
	 */
	@PostMapping("/materialNewsUpload")
	@PreAuthorize("@ss.hasPermi('wx:wxassets:save')")
	@ApiOperation(value = "添加图文永久素材")
	public AjaxResult materialNewsUpload(@RequestParam String appid,@RequestBody List<WxMpNewsArticle> articles) throws WxErrorException {
		if(articles.isEmpty()) {
			return AjaxResult.error("图文列表不得为空");
		}
		WxMpMaterialUploadResult res = wxAssetsService.materialNewsUpload(appid,articles);
		return AjaxResult.success(res);
	}

	/**
	 * 修改图文素材文章
	 *
	 * @param form
	 * @return
	 * @throws WxErrorException
	 */
	@PostMapping("/materialArticleUpdate")
	@PreAuthorize("@ss.hasPermi('wx:wxassets:save')")
	@ApiOperation(value = "修改图文素材文章")
	public AjaxResult materialArticleUpdate(@RequestParam String appid,@RequestBody WxMpMaterialArticleUpdate form) throws WxErrorException {
		if(form.getArticles()==null) {
			return AjaxResult.error("文章不得为空");
		}
		wxAssetsService.materialArticleUpdate(appid,form);
		return AjaxResult.success();
	}

	/**
	 * 添加多媒体永久素材
	 *
	 * @param file
	 * @param fileName
	 * @param mediaType
	 * @return
	 * @throws WxErrorException
	 * @throws IOException
	 */
	@PostMapping("/materialFileUpload")
	@PreAuthorize("@ss.hasPermi('wx:wxassets:save')")
	@ApiOperation(value = "添加多媒体永久素材")
	public AjaxResult materialFileUpload(@RequestParam String appid, MultipartFile file, String fileName, String mediaType) throws WxErrorException, IOException {
		if (file == null) {
			return AjaxResult.error("文件不得为空");
		}
		WxMpMaterialUploadResult res = wxAssetsService.materialFileUpload(appid,mediaType,fileName,file);
		return AjaxResult.success();
	}

	/**
	 * 删除素材
	 *
	 * @param form
	 * @return
	 * @throws WxErrorException
	 */
	@PostMapping("/materialDelete")
	@PreAuthorize("@ss.hasPermi('wx:wxassets:delete')")
	@ApiOperation(value = "删除素材")
	public AjaxResult materialDelete(@RequestParam String appid,@RequestBody MaterialFileDeleteForm form) throws WxErrorException {
		boolean res = wxAssetsService.materialDelete(appid,form.getMediaId());
		return AjaxResult.success(res);
	}

}
